﻿using B.S.Outsourcing.Domain;
using B.S.Outsourcing.ErrorCode;
using B.S.Outsourcing.Infatructrue;
using B.S.Outsourcing.Writer.Api.Application.Command.OutSourOrderModel;
using MediatR;

namespace B.S.Outsourcing.Writer.Api.Application.CommandHandler.OutSourOrderModel
{
    public class CreateOutSourOrderCommandHandler : IRequestHandler<CreateOutSourOrderCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<OutsourcingOrderModel> baseRepository;
        private readonly DBHelper1 dbHelper;

        public CreateOutSourOrderCommandHandler(IBaseRepository<OutsourcingOrderModel> baseRepository, DBHelper1 dbHelper)
        {
            this.baseRepository = baseRepository;
            this.dbHelper = dbHelper;
        }

        public async Task<ApiResult<int>> Handle(CreateOutSourOrderCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            request.RequirementDate = request.RequirementDate.Value.AddHours(8);
            var sql = $"insert into OutsourcingOrderModel values('{request.DocumentCode}','{request.BusinessType}','{request.PlanOrderNumber}','{request.MaterialCode}','{request.Supplier}','{request.DocumentDate}','{request.Operator}','{request.SalesOrderNumber}','{request.MaterialName}','{request.EntrustedContract}','{request.DocumentStatus}','{request.Auditor}','{request.OrderDeliveryDate}','{request.SpecificationModel}','{request.OutsourcingQuantity}','{request.BusinessStatus}','{request.AuditDate}','{request.RequirementDate}','{request.DrawingNumber}','{request.ProcessingFee}','{request.Remarks}','{request.CreatedDate}','{request.CreatedBy}','{request.ModifiedDate}','{request.ModifiedBy}','{request.IsDelete}')";
            var str = dbHelper.NonQuery(sql);
            if (str > 0)
            {
                result.Code = ApiEnums.成功;
                result.Message = "成功";
                result.Data = str;
            }
            else
            {
                result.Code = ApiEnums.失败;
                result.Message = "失败";
                result.Data = str;
            }
            return await Task.FromResult(result);
        }
    }
}
